<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class CartItem extends Model
{
    use HasFactory;

    protected $fillable = [
        'cart_id',
        'product_id',
        'variant_id',
        'quantity',
        'price',
        'price_at_add',
        'current_price',
        'price_updated_at',
        'specifications',
        'image',
        'is_selected',
    ];

    protected $casts = [
        'specifications' => 'array',
        'price' => 'decimal:2',
        'price_at_add' => 'decimal:2',
        'current_price' => 'decimal:2',
        'price_updated_at' => 'datetime',
        'is_selected' => 'boolean',
    ];

    /**
     * 获取所属购物车
     */
    public function cart(): BelongsTo
    {
        return $this->belongsTo(Cart::class);
    }

    /**
     * 获取商品信息
     */
    public function product(): BelongsTo
    {
        return $this->belongsTo(Product::class);
    }

    /**
     * 获取商品变体信息
     */
    public function variant(): BelongsTo
    {
        return $this->belongsTo(ProductVariant::class);
    }

    /**
     * 获取小计金额
     */
    public function getSubtotalAttribute(): float
    {
        return $this->price * $this->quantity;
    }

    /**
     * 获取规格文本
     */
    public function getSpecificationsTextAttribute(): string
    {
        if (!$this->specifications) {
            return '';
        }
        
        return collect($this->specifications)
            ->map(function ($spec) {
                return $spec['name'] . ': ' . $spec['value'];
            })
            ->join(', ');
    }

    /**
     * 检查价格是否变动
     */
    public function hasPriceChanged(): bool
    {
        if (!$this->price_at_add) {
            return false;
        }

        $currentPrice = $this->variant ? $this->variant->price : $this->product->price;
        return $currentPrice != $this->price_at_add;
    }

    /**
     * 更新当前价格
     */
    public function updateCurrentPrice(): void
    {
        $currentPrice = $this->variant ? $this->variant->price : $this->product->price;
        
        $this->update([
            'current_price' => $currentPrice,
            'price_updated_at' => now(),
        ]);
    }

    /**
     * 获取价格变动信息
     */
    public function getPriceChangeInfo(): array
    {
        if (!$this->hasPriceChanged()) {
            return [
                'changed' => false,
                'original_price' => $this->price_at_add,
                'current_price' => $this->price_at_add,
                'change_amount' => 0,
                'change_percentage' => 0,
            ];
        }

        $currentPrice = $this->variant ? $this->variant->price : $this->product->price;
        $changeAmount = $currentPrice - $this->price_at_add;
        $changePercentage = round(($changeAmount / $this->price_at_add) * 100, 2);

        return [
            'changed' => true,
            'original_price' => $this->price_at_add,
            'current_price' => $currentPrice,
            'change_amount' => $changeAmount,
            'change_percentage' => $changePercentage,
        ];
    }
}
